package com.sdy.mvc.aop;

import com.sdy.mvc.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * Created by zzq on 2018/4/8.
 */
@Slf4j
public class RemoteServiceLogHandler {

    /**
     * Service调用日志
     *
     * @param point
     */
    public void before(JoinPoint point) {
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] fieldNames = methodSignature.getParameterNames();
        Object[] fieldValues = point.getArgs();
        StringBuilder params = new StringBuilder();
        try {
            if (fieldNames != null) {
                for (int i = 0; i < fieldNames.length; i++) {
                    if (!(fieldValues[i] instanceof Class)) {
                        if (i > 0) {
                            params.append(",");
                        }
                        params.append(fieldNames[i]).append("=").append(JsonUtil.toJson(fieldValues[i]));
                    }
                }
            }
        } catch (Exception e) {
            log.error("RemoteServiceLogHandler error!", e);
        }
        log.info("Remote: {}.{}, param:[{}].", point.getTarget().toString(), point.getSignature().getName(), params.toString());
    }
}
